﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BasePlatformNet
{
    public struct ChannelState
    {
        public enum DataType
        { 
            DMX512 = 0,
            MIDI = 1,
            Avab = 2,
            Colortran_CMX = 3,
            ADB_625 = 4,
            Art_Net = 5
        }

        public DataType datatype;
        public bool InputFlag;
        public bool OutputFlag;

        public ChannelState(bool input, bool output)
        {
            datatype = DataType.DMX512;
            InputFlag = input;
            OutputFlag = output;
        }

        public byte generatebyte()
        {
            byte code = 0;

            code |= (byte)datatype;
            if (InputFlag) code |= (1 << 6);
            if (OutputFlag) code |= (1 << 7);

            return code;
        }

        public ChannelState(byte code)
        {
            int dataTypeCode = code & 0x3f;
            if (Enum.IsDefined(typeof(DataType), dataTypeCode))
            {
                datatype = (DataType)Enum.ToObject(typeof(DataType), dataTypeCode);
            }
            else
            {
                datatype = DataType.DMX512;
            }

            InputFlag = !((code & (1 << 6)) == 0);
            OutputFlag = !((code & (1 << 7)) == 0);

        }
    }

    public struct ArtInputState
    {
        public bool enable;

        public bool error;

        public bool Received;
        public bool DMX_Test;
        public bool DMX_SIP;
        public bool DMX_Text;

        public byte generatebyte()
        {
            byte code = 0;

            if (error) code |= (1 << 2);
            if (enable) code |= (1 << 3);
            if (DMX_Text) code |= (1 << 4);
            if (DMX_SIP) code |= (1 << 5);
            if (DMX_Test) code |= (1 << 6);
            if (Received) code |= (1 << 7);

            return code;
        }

        public ArtInputState(bool enable)
        {
            this.enable = enable;
            this.error = false;
            this.DMX_SIP = false;
            this.DMX_Test = false;
            this.DMX_Text = false;
            this.Received = false;
            this.error = false;
        }

        public ArtInputState(byte code)
        {
            error = !((code & (1 << 2)) == 0);
            enable = !((code & (1 << 3)) == 0);
            DMX_Text = !((code & (1 << 4)) == 0);
            DMX_SIP = !((code & (1 << 5)) == 0);
            DMX_Test = !((code & (1 << 6)) == 0);
            Received = !((code & (1 << 7)) == 0);
        }
    }

    public struct ArtOutputState
    { 
        public bool Transmitted;
        public bool DMX_Test;
        public bool DMX_SIP;
        public bool DMX_Text;
        public bool merging;
        public bool DMXOnStart;
        public bool LTPMerge;
        public bool sACN_ArtNet;

        public byte generatebyte()
        {
            byte code = 0;
            if (sACN_ArtNet) code |= (1);
            if (LTPMerge) code |= (1 << 1);
            if (DMXOnStart) code |= (1 << 2);
            if (merging) code |= (1 << 3);
            if (DMX_Text) code |= (1 << 4);
            if (DMX_SIP) code |= (1 << 5);
            if (DMX_Test) code |= (1 << 6);
            if (Transmitted) code |= (1 << 7);

            return code;
        }

        public ArtOutputState(byte code)
        {
            sACN_ArtNet = !((code & (1)) == 0);
            LTPMerge = ((code & (1 << 1)) == 0);
            DMXOnStart = !((code & (1 << 2)) == 0);
            merging = !((code & (1 << 3)) == 0);
            DMX_Text = !((code & (1 << 4)) == 0);
            DMX_SIP = !((code & (1 << 5)) == 0);
            DMX_Test = !((code & (1 << 6)) == 0);
            Transmitted = !((code & (1 << 7)) == 0);
        }
    }
}
